package com.cw.jeeyt.common.utils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang.StringEscapeUtils;
import org.apache.commons.lang3.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.safety.Whitelist;

/**
 * @author chenw
 */
public class Html2PlainText {
    /**
     * html转换成txt文件
     *
     * @param html
     * @return
     */
    public static String convert(String html) {
        if (StringUtils.isEmpty(html)) {
            return "";
        }
        Document document = Jsoup.parse(html);
        Document.OutputSettings outputSettings = new Document.OutputSettings().prettyPrint(false);
        document.outputSettings(outputSettings);
        document.select("br")
            .append("\\n");
        // document.select("p")
        //     .prepend("\\n");
        document.select("p")
            .append("\\n");
        String newHtml = document.html()
            .replaceAll("\\\\n", "\n");
        String plainText = Jsoup.clean(newHtml, "", Whitelist.none(), outputSettings);
        String result = StringEscapeUtils.unescapeHtml(plainText.trim());
        Pattern p = Pattern.compile("(\r?\n(\\s*\r?\n)+)");
        Matcher m = p.matcher(result);
        result = m.replaceAll("\r\n");
        result = result.replaceAll("\u00A0", "");
        return result;
    }

    public static void main(String[] args) {
        String html = "<p><span style=\"white-space: nowrap;\">package ${params.packageName}.${params.moduleId}.biz.bo;</span></p><p><span style=\"white-space: nowrap;\"><br/></span></p><p><span style=\"white-space: nowrap;\">import java.io.Serializable;</span></p><p><span style=\"white-space: nowrap;\"><br/></span></p><p><span style=\"white-space: nowrap;\">import lombok.Builder;</span></p><p><span style=\"white-space: nowrap;\">import lombok.Data;</span></p><p><span style=\"white-space: nowrap;\"><br/></span></p><p><span style=\"white-space: nowrap;\">/**</span></p><p><span style=\"white-space: nowrap;\">&nbsp;* ${params.tableCnName}</span></p><p><span style=\"white-space: nowrap;\">&nbsp;*</span></p><p><span style=\"white-space: nowrap;\">&nbsp;* @author ${params.author}</span></p><p><span style=\"white-space: nowrap;\">&nbsp;* @date ${params.createDate}</span></p><p><span style=\"white-space: nowrap;\">&nbsp;*/</span></p><p><span style=\"white-space: nowrap;\">@Data</span></p><p><span style=\"white-space: nowrap;\">@Builder</span></p><p><span style=\"white-space: nowrap;\">public class ${params.className}Bo implements Serializable {</span></p><p><span style=\"white-space: nowrap;\">&nbsp; &nbsp; private static final long serialVersionUID = ${params.serialId};</span></p><p><span style=\"white-space: nowrap;\">&nbsp; &nbsp; #foreach ($fld in $params.fieldList)</span></p><p><span style=\"white-space: nowrap;\">&nbsp; &nbsp; /** $fld.fieldCnName */</span></p><p><span style=\"white-space: nowrap;\">&nbsp; &nbsp; #if($fld.isDict ==&#39;Y&#39;)</span></p><p><span style=\"white-space: nowrap;\">&nbsp; &nbsp; private $fld.dictCode $fld.fieldName;</span></p><p><span style=\"white-space: nowrap;\">&nbsp; &nbsp; #else</span></p><p><span style=\"white-space: nowrap;\">&nbsp; &nbsp; private $fld.fieldType $fld.fieldName;</span></p><p><span style=\"white-space: nowrap;\">&nbsp; &nbsp; #end</span></p><p><span style=\"white-space: nowrap;\">&nbsp; &nbsp; #end</span></p><p><span style=\"white-space: nowrap;\">}</span>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;</p>";
        System.out.println(convert(html));
    }
}